﻿namespace Home.Services
{
    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Data.Entity.ModelConfiguration;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    public static class RepositoryHelper
    {
        public static EntityTypeConfiguration<TEntityType> CreateEntity<TEntityType>(this DbModelBuilder builder, string tableName)
            where TEntityType : Entity
        {
            Debug.Assert(builder != null);
            Debug.Assert(!string.IsNullOrEmpty(tableName));

            var entity = builder.Entity<TEntityType>();

            entity.ToTable(tableName);

            entity.HasKey(m => m.Id)
                  .Property(m => m.Id)
                  .HasColumnName("id")
                  .IsRequired();

            entity.Property(m => m.Created)
                  .HasColumnName("created")
                  .IsRequired();

            entity.Property(m => m.CreatedBy)
                  .HasColumnName("created_by")
                  .IsRequired()
                  .HasMaxLength(64)
                  .IsUnicode(false)
                  .IsVariableLength();

            entity.Property(m => m.Modified)
                  .HasColumnName("modified")
                  .IsRequired();

            entity.Property(m => m.ModifiedBy)
                  .HasColumnName("modified_by")
                  .IsRequired()
                  .HasMaxLength(64)
                  .IsUnicode(false)
                  .IsVariableLength();

            entity.Property(m => m.RowVersion)
                  .HasColumnName("row_version")
                  .IsRowVersion();

            return entity;
        }
    }
}
